/**
 * @Author:卿雨欣
 * @Date:20200617
 * @Describle:这是符合策略限制条件的股票列表页
 */
import React, { Component } from "react";
import {Table, Button, Modal} from "antd";
import axios from "axios"
import {URL} from '../../../index'
//声明全局变量、常量
//表格的表头
const stockColumns = [
    {
        title: '名称',
        dataIndex: 'shareName',
    },
    {
        title: '代码',
        dataIndex: 'shareTsCode',
    },
    {
        title: '行业',
        dataIndex: 'shareIndustry',
    },
    {
        title: '市场类型',
        dataIndex: 'shareMarket',
    },
];
// 将获取的数据中属性为数字的转换为文字
function numberName(newList){
    for(let i=0;i<newList.length;i++) {
        switch (newList[i].shareIndustry) {
            case 8:
                newList[i].shareIndustry = "软件服务";
                break;
            case 9:
                newList[i].shareIndustry = "电气设备";
                break;
            case 10:
                newList[i].shareIndustry = "汽车配件";
                break;
            case 11:
                newList[i].shareIndustry = "元器件";
                break;
            case 12:
                newList[i].shareIndustry = "互联网";
                break;
            case 13:
                newList[i].shareIndustry = "环境保护";
                break;
        }
        switch (newList[i].shareMarket) {
            case 14:
                newList[i].shareMarket ="创业板";
                break;
            case 15:
                newList[i].shareMarket ="主板";
                break;
            case 16:
                newList[i].shareMarket ="中小板";
                break;
        }
    }
    return newList;
};
export default class StockTable extends Component{

    constructor(props) {
        super(props);
        this.state = {
            selectedRowKeys: [], // 选中的行的key
            loading: false,
            selectedNames:" ", //选中的行
            shareList:[], //选中的产品的名称平凑的字符串
            products1:[], //后端获取到的股票数组
            shareLength:0, //策略列表规定的股票产品数量
        };
    }
    componentWillMount() {
        axios({
            method:"GET",
            url:URL+"/strategy/strategyFiltrate",
            params:{strategyId:this.props.strategyIdStock,},
        }).then(res=>{
            if(res.data.code == 500){
                this.props.history.push('/serverError')
            }else {
                //更新声明的状态
                this.setState({
                    shareList:numberName(res.data.shareList), //使用函数，将数字转化为文字
                    shareLength:res.data.shareLength,
                })
                this.props.onStocksLength(res.data.shareLength); // 给父组件传值，策略规定的股票产品数量
            }
        }).catch(error=>{
            console.log(error);
        });
    }
    // 重置函数
    start = (selectedRows) => {
        this.setState({ loading: true });
        setTimeout(() => {
            this.setState({
                selectedRowKeys: [],
                loading: false,
                selectedRows:[],
                selectedNames:" ",
            });
        }, 1000);
        this.props.onStocks([]);
    };
    // 选择列表项，保存选中的项
    onSelectChange = (selectedRowKeys,selectedRows,selectedNames,shareList)=> {
        //选择的数量小于策略规定的股票数量
        if(selectedRowKeys.length<=this.state.shareLength){
            let names = ""; //变量，拼接的选中产品的名称
            //拼接选中的产品的名称
            for(let i=0;i< selectedRowKeys.length;i++){
                let item = selectedRowKeys[i];
                for(let i2=0;i2<this.state.shareList.length;i2++){
                    if(this.state.shareList[i2].shareId === item){
                        names += "【"+this.state.shareList[i2].shareName+"】"+" ";
                    }
                }
            }
            this.setState({
                selectedRowKeys,
                selectedRows,
                selectedNames:names,
            });
            this.props.onStocks(selectedRows);
        }else{
            Modal.warning({
                title: '股票数量提示',
                content: '该策略股票数量为'+this.state.shareLength+",请选择该数量的股票",
            })
        }
    };

    render() {
        //声明render用到的变量、常量
        const { loading, selectedRowKeys } = this.state;
        const rowSelection = {
            selectedRowKeys,
            onChange: this.onSelectChange,
        };
        const hasSelected = selectedRowKeys.length > 0;
        return (
            <div>
                <div style={{width:"40%",float:"right",marginRight:"5%"}}>
                    <div style={{ marginBottom: 16}}>
                        <Button type="primary" onClick={this.start} disabled={!hasSelected} loading={loading}>
                            清除
                        </Button>
                        <span style={{ marginLeft: 8 }}>
                            {hasSelected ? `已选 ${selectedRowKeys.length} 支股票` : ''}
                            {hasSelected ? ` ${ this.state.selectedNames} ` : ''}
                        </span>
                    </div>
                    <Table rowSelection={rowSelection}
                           pagination={{pageSize:5}}
                           columns={stockColumns}
                           dataSource={this.state.shareList}
                           rowKey="shareId"/>
                </div>
            </div>
        )
    }
}
